// 语言包
var chi = require('../../utils/Chinese.js')
var eng = require("../../utils/English.js")
//获取应用实例
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        language: '',
        // 语言包对象
        content: '',
        // tab style
        line_height:'',
        // tab 切换
        currentTab: 0,
        winWidth: 0,
        winHeight: 0,
        // 全部订单
        order_list: [{ id: '1', state: '1', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] }, { id: '2', state: '2', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] }],
        // 待付款 状态值1
        pay_list: [{ id: '1', state: '1', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] },],
        // 待收货 状态值2
        goods_list: [{ id: '2', state: '2', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] }],
        // 已完成 状态值3
        completed_list: [{ id: '3', state: '3', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] }],
        // 已取消状态值4
        cancel_list: [{ id: '4', state: '4', money: '127', orderNo: '12711111', order_time: "", product: [{ id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }, { id: "0", url: "../../img/tui_03.jpg", name: "焦糖味糖浆 意大利原装", price: 127, capacity: '1000ml', number: "1" }] }],
        // 消息提示
        pay_infor: "",
        goods_infor: "",

    },

    on: function () {
        let that = this
        this.setData({
            language: app.data.language
        })
        var lastLanuage = that.data.language
        if (lastLanuage == 'zh_CN') {
            that.setData({
                content: chi.content,
                line_height:'80rpx'
            })
            wx.setNavigationBarTitle({
                title: '我的订单'
            })
        } else if (lastLanuage == 'zh_EN') {
            that.setData({
                content: eng.content,
                line_height:'30rpx'
            })
            // 修改导航标题
            wx.setNavigationBarTitle({
                title: 'My Order'
            })
        }
    },
    //  tab切换逻辑
    swichNav: function (e) {

        var that = this;

        if (this.data.currentTab === e.target.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.target.dataset.current
            })
        }
        if (e.target.dataset.current == 1) {
            that.setData({
                pay_infor: 0
            })
        } else if (e.target.dataset.current == 2) {
            that.setData({
                goods_infor: 0
            })
        }
    },
    bindChange: function (e) {

        var that = this;
        that.setData({
            currentTab: e.detail.current
        });

    },
    // 跳转
    product: function (e) {
        console.log(e)
        wx.navigateTo({
            url: '../order_detail/order_detail',
        })
    },
    logistics: function () {
        wx.navigateTo({
            url: '../express/express',
        })
    },
    confirm: function () {
        var that = this
        var lastLanuage = that.data.language
        if (lastLanuage == 'zh_CN') {
            wx.showModal({
                cancelText: "取消", //默认是“取消”
                confirmText: "确定", //默认是“确定”
                title: '提示',
                content: '您是否要确认收货？',
                success: function (res) {
                    if (res.confirm) {
                        // 点击确定
                        console.log('点击了确定')
                    } else if (res.cancel) {
                    }
                }
            })
        } else if (lastLanuage == 'zh_EN') {
            wx.showModal({
                title: 'Tips',
                cancelText: "NO", //默认是“取消”
                confirmText: "YES", //默认是“确定”
                content: 'Do you want to confirm receipt ?',
                success: function (res) {
                    if (res.confirm) {
                        //   点击确定
                        console.log('点击了确定')
                    } else if (res.cancel) {
                    }
                }
            })
        }
    },
    del: function (e) {
        var that = this
        //得到下标
        console.log(e)
        var state = e.currentTarget.dataset.state
        var index = e.currentTarget.dataset.index
        var comList = that.data.completed_list
        var cancelList = that.data.cancel_list
        var lastLanuage = that.data.language
        if (lastLanuage == 'zh_CN') {
            wx.showModal({
                cancelText: "取消", //默认是“取消”
                confirmText: "确定", //默认是“确定”
                title: '提示',
                content: '确认删除此订单吗？删除后不可恢复！',
                success: function (res) {
                    if (res.confirm) {
                        if (state == 3) {
                            comList.splice(index, 1)
                        } else if (state == 4) {
                            cancelList.splice(index, 1)
                        }
                        //把新的数组传给前台
                        that.setData({
                            cancel_list: cancelList,
                            completed_list: comList
                        })
                    } else if (res.cancel) {
                    }
                }
            })
        } else if (lastLanuage == 'zh_EN') {
            wx.showModal({
                title: 'Tips',
                cancelText: "NO", //默认是“取消”
                confirmText: "YES", //默认是“确定”
                content: 'Are you sure to delete this order? Cannot be restored after deletion!',
                success: function (res) {
                    if (res.confirm) {
                        if (state == 3) {
                            comList.splice(index, 1)
                        } else if (state == 4) {
                            cancelList.splice(index, 1)
                        }
                        //把新的数组传给前台
                        that.setData({
                            cancel_list: cancelList,
                            completed_list: comList
                        })
                    } else if (res.cancel) {
                    }
                }
            })
        }
    },
    // END ===========================
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        that.on()
        var pay = that.data.pay_list.length
        var goods = that.data.goods_list.length
        that.setData({
            currentTab: app.data.currentTab,
            pay_infor: pay,
            goods_infor: goods
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (e) {
        let that = this
        that.on()
        if (that.data.currentTab == 1) {
            that.setData({
                pay_infor: 0
            })
        } else if (that.data.currentTab == 2) {
            that.setData({
                goods_infor: 0
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})